package com.hh.game.common.base.manager.session;

import io.netty.channel.Channel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GameSession implements Session {
    private String id;
    private Channel channel;
    private boolean login;
    private Map<String ,Object> parameterMap = new HashMap<>();

    private List<SessionClose> closeListers = new ArrayList<>();
    private List<SessionLogin> loginListers = new ArrayList<>();

    public GameSession(String id, Channel channel) {
        this.id = id;
        this.channel = channel;
    }

    public void onClose(){
        closeListers.forEach(close -> {
            close.onClose(this);
        });
    }

    public void onLogin(){
        loginListers.forEach(login -> {
            login.onLogin(this);
        });
    }

    public void cancelSessionClose(SessionClose sessionClose){
        closeListers.remove(sessionClose);
    }

    public void registersessionCloses(SessionClose sessionClose){
        closeListers.add(sessionClose);
    }


    @Override
    public String getId() {
        return id;
    }

    @Override
    public boolean isOpen() {
        return channel.isOpen();
    }

    @Override
    public void login(){
        this.login = true;
        onLogin();
    }

    @Override
    public boolean isLogin() {
        return this.login;
    }

    @Override
    public void close() {
        onClose();
        channel.close();
    }

    @Override
    public Channel channel() {
        return channel;
    }

    @Override
    public Map<String, Object> getParameterMap() {
        return parameterMap;
    }

    @Override
    public void addParameter(String key, Object val) {
        parameterMap.put(key ,val);
    }

    @Override
    public Object getParameter(String key) {
        return parameterMap.get(key);
    }

    @Override
    public void sendClient(String route, Object obj) {

    }
}
